package com.smzd.utils.email;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.smzd.utils.freemarker.FreeMarkerHelper;

public class EmailService implements InitializingBean {

    private static Logger logger = Logger.getLogger(EmailService.class);
    private JavaMailSender mailSender;
    private FreeMarkerHelper freeMarkerHelper;

    private String host;
    private int port = 25;
    private String protocol = "smtp";
    private String username;
    private String password;

    public void sendTextEmail(String content, String title, String receivers) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setText((content));
        simpleMailMessage.setSubject((title));
        simpleMailMessage.setFrom(username);
        simpleMailMessage.setTo(receivers);
        simpleMailMessage.setSentDate(new Date());
        mailSender.send(simpleMailMessage);

    }

    /**
     * @param template
     * @param model
     * @param title
     * @param receivers
     * @throws Exception
     */
    public void sendTemplateEmail(String template, Map<String, Object> model, String title, String receivers)
            throws Exception {
        String stringWriter = mergeTemplateContent(template, model);
        sendTextEmail(stringWriter, title, receivers);
    }

    /**
     * @param template
     * @param model
     * @return
     * @throws Exception
     */
    private String mergeTemplateContent(String template, Map<String, Object> model) throws Exception {
        return freeMarkerHelper.renderByView(template, model);
    }

    /**
     * @param template
     * @param model
     * @param title
     * @param receivers
     * @throws Exception
     */
    public void sendTemplateEmail(String template, Map<String, Object> model, String title, List<String> receivers)
            throws Exception {
        for (String toUser : receivers) {

            sendTemplateEmail(template, model, title, toUser);
        }
    }

    /**
     * @param template
     * @param model
     * @param title
     * @param receivers
     * @throws Exception
     */
    public void sendHtmlTemplateEmail(String template, Map<String, Object> model, String title, List<String> receivers)
            throws Exception {

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true, "UTF-8");
        String content = mergeTemplateContent(template, model);
        mimeMessageHelper.setSentDate(new Date());
        mimeMessageHelper.setFrom(username);
        mimeMessageHelper.setText(content, content);
        mimeMessageHelper.setSubject((title));
        for (String rec : receivers) {
            mimeMessageHelper.setTo(rec);
            mailSender.send(mimeMessageHelper.getMimeMessage());
        }
    }

    /**
     * @param template
     * @param model
     * @param title
     * @param receiver
     * @throws Exception
     */
    public void sendHtmlTemplateEmail(String template, Map<String, Object> model, String title, String receiver)
            throws Exception {

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true, "UTF-8");

        String content = mergeTemplateContent(template, model);
        mimeMessageHelper.setSentDate(new Date());
        mimeMessageHelper.setFrom(username);
        mimeMessageHelper.setText(content, content);
        mimeMessageHelper.setSubject((title));
        mimeMessageHelper.setTo(receiver);
        mailSender.send(mimeMessageHelper.getMimeMessage());

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        mailSender.setProtocol(protocol);
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true"); // 这样才能通过验证
        mailSender.setJavaMailProperties(props);
        this.mailSender = mailSender;
    }

    public FreeMarkerHelper getFreeMarkerHelper() {
        return freeMarkerHelper;
    }

    public void setFreeMarkerHelper(FreeMarkerHelper freeMarkerHelper) {
        this.freeMarkerHelper = freeMarkerHelper;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
